package com.xinhe.web.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.QRole;
import com.xinhe.web.entity.Role;
import com.xinhe.web.reposity.AuthRoleRepository;
import com.xinhe.web.util.Snowflake;
import com.xinhe.web.vo.RoleView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.inject.Provider;
import javax.persistence.EntityManager;
import java.util.Date;
import java.util.Objects;

@Service
public class RoleService {
    //初始化查询工厂
    private JPAQueryFactory queryFactory;
    @Autowired
    private Provider<EntityManager> entityManager;
    @PostConstruct
    public void init() {
        queryFactory = new JPAQueryFactory(entityManager);
    }
    @Autowired
    AuthRoleRepository authRoleRepository;

    public QueryResults<RoleView> pageRole(String keyword, Integer terminal, Long parentRoleId, Long platformUserId,
                                           int pageNum, int pageSize) {
        QRole qRole = QRole.role;
        BooleanBuilder builder = new BooleanBuilder();
        if (!Objects.isNull(keyword)){
            builder.and(qRole.name.like(keyword));
        }
        if (!Objects.isNull(terminal)){
            builder.and(qRole.terminal.eq(terminal));
        }
        if (!Objects.isNull(parentRoleId)){
            builder.and(qRole.fParentRoleId.eq(parentRoleId));
        }
        if (!Objects.isNull(platformUserId)){
            builder.and(qRole.fPlatformUserId.eq(platformUserId));
        }
        return queryFactory.select(
                Projections.bean(
                        RoleView.class,
                        qRole.id,
                        qRole.name,
                        qRole.terminal,
                        qRole.fParentRoleId,
                        qRole.fPlatformUserId,
                        qRole.createTime,
                        qRole.remark
                )
        ).from(qRole)
                .where(builder).offset(pageNum * pageSize).limit(pageSize).fetchResults();
    }

    public Result<Role> findById(Long roleId) {
        return Result.ok(authRoleRepository.findById(roleId).get());
    }

    public Result delete(Long roleId) {
        authRoleRepository.deleteById(roleId);
        return Result.ok();
    }

    public Result saveModel(String name, Integer terminal, Long parentRoleId, Long platformUserId, String remark) {
        Role role = new Role();
        role.setId(Snowflake.generateId());
        role.setName(name);
        role.setTerminal(terminal);
        role.setfParentRoleId(parentRoleId);
        role.setfPlatformUserId(platformUserId);
        role.setRemark(remark);
        role.setRemoved(0);
        role.setCreateTime(new Date());
        authRoleRepository.save(role);
        return Result.ok();
    }
}
